package com.gosophia.conference.action.common;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.gosophia.commons.exception.BusinessException;
import com.gosophia.conferenceManagement.ConferenceConfiguration;
import com.gosophia.conferenceManagement.common.dto.ConferenceJobDataDTO;
import com.gosophia.conferenceManagement.common.ws.ConferenceMissionDetailWebService;
import com.gosophia.conferenceManagement.common.ws.ConferenceQueryWebService;
import com.gosophia.conferenceManagement.starting.dto.ConferenceStartingDTO;
import com.gosophia.dataDictionary.dto.DataValueDTO;
import com.gosophia.dataDictionary.ws.DataDictionaryWebService;
import com.gosophia.taskManagement.action.BaseMissionDetailAction;
import com.gosophia.taskManagement.dto.JobDTO;
import com.gosophia.taskManagement.dto.JobHandleRecordsDTO;
import com.gosophia.taskManagement.ws.TaskManagementWebService;
/**
 * 会议Mission详细信息
 * @author wangxinbing
 * @since  2011-4-6
 *  Copyright (c) 2010 Gosophia corporation All Rights Reserved
 */
@Component("GetConferenceMissionDetailAction")
@Scope("prototype")
public class GetConferenceMissionDetailAction extends BaseMissionDetailAction {

	//会议任务详细
	private ConferenceStartingDTO conferenceStartingDTO;
	
	private String importanceLevel;
	@Autowired
	private ConferenceMissionDetailWebService conferenceMissionDetailWebService;
	@Autowired
	private ConferenceQueryWebService conferenceQueryWebService;
	@Autowired
	private DataDictionaryWebService dataDictionaryWebService;
	@Autowired
	private TaskManagementWebService taskManagementWebService;

	private List<JobHandleRecordsDTO> historyList = new ArrayList<JobHandleRecordsDTO>();
	private final String conferenceApplicationModel = ConferenceConfiguration.NEED_APPLICATION_CONFERENCE;
	@Override
	public String _execute() throws Exception {
		conferenceStartingDTO =conferenceMissionDetailWebService.
		                      findConferenceMissionDetailByMissionId(super.getMissionId());
		if(conferenceStartingDTO !=null && conferenceStartingDTO.getImportentLevel() !=null){
			DataValueDTO importance = dataDictionaryWebService.findDataValueByDataValueId(conferenceStartingDTO.getImportentLevel());
			importanceLevel = importance.getValue();
		}
		getHistoryLists();
		return SUCCESS;
	}

	private void getHistoryLists() throws Exception {
		List<JobHandleRecordsDTO> hislist = new ArrayList<JobHandleRecordsDTO>();
		hislist = super.getHistoryList();
		JobDTO jobDto = new JobDTO();
		if(hislist!=null && hislist.size()>1){
			historyList.add(hislist.get(0));
			for(int i =1;i<hislist.size();i++){
				JobHandleRecordsDTO jh = hislist.get(i);
				String jobType = jh.getJobType();
				try {
					jobDto = taskManagementWebService.findJobDetailByJobId(jh.getJobId());
				} catch (BusinessException e) {
					e.printStackTrace();
				}
				if(jobDto!=null){
					ConferenceJobDataDTO conferenceJobDataDTO = conferenceMissionDetailWebService.findConferenceJobDataByJobId(jobDto.getJobId());
					if(jobType!=null){
						//设置发起content
						setStartContent(jh, jobType);
						//设置申请Content
						setApplyContent(jh, jobType, conferenceJobDataDTO);
						//设置方案Content
						setPlanContent(jh, jobType, conferenceJobDataDTO);
						//设置其他Content
						setOthersContent(jh, jobType, conferenceJobDataDTO);
					    //设置会务准备Content
						setConferencePrepareContent(jh, jobType);
		                //设置预通知Content
						setPreNoticeContent(jh, jobType, conferenceJobDataDTO);
						//设置正式通知Content
						setNoticeContent(jh, jobType, conferenceJobDataDTO);
						//设置会议总结Content
						setSummaryContent(jh, jobType, conferenceJobDataDTO);
					}
				}
			}
		}else if(hislist.size()==1){
			historyList.add(hislist.get(0));
		}
	}
	
	private void setSummaryContent(JobHandleRecordsDTO jh, String jobType,
			ConferenceJobDataDTO conferenceJobDataDTO) {
		String underTaker = getUnderTaker(conferenceJobDataDTO);
		if("总结起草".equals(jobType)){
			jh.setContent("起草了[会议总结]");
			historyList.add(jh);
			setToapprovalContent(jh, underTaker);
		}
		if("总结修改".equals(jobType)){
			jh.setContent("修订了[会议总结]提交给  "+ underTaker +" 进行审批");
			historyList.add(jh);
		}
		if("总结定稿".equals(jobType)){
			jh.setContent("会议总结定稿");
			historyList.add(jh);
		}
		if("总结发送".equals(jobType)){
			jh.setContent("会议总结发送");
			//jh.setDetailURL(null);
			historyList.add(jh);
		}

	}

	/**
	 * @param conferenceJobDataDTO
	 * @return
	 */
	private String getUnderTaker(ConferenceJobDataDTO conferenceJobDataDTO) {
		String underTaker = null;
		if(conferenceJobDataDTO !=null && conferenceJobDataDTO.getNextApproval() !=null){
		   underTaker=	conferenceJobDataDTO.getNextApproval().getUserName();
		}
		return underTaker;
	}

	private void setNoticeContent(JobHandleRecordsDTO jh, String jobType,
			ConferenceJobDataDTO conferenceJobDataDTO) {
		String underTaker = getUnderTaker(conferenceJobDataDTO);
		if("正式通知起草".equals(jobType)){
			jh.setContent("起草了[正式通知]");
			historyList.add(jh);
			setToapprovalContent(jh, underTaker,(long)(jh.getLevel()+1));
		}
		if("正式通知修改".equals(jobType)){
			jh.setContent("修订了[正式通知]提交给  "+ underTaker +" 进行审批");
			historyList.add(jh);
		}
		if("正式通知定稿".equals(jobType)){
			jh.setContent("正式通知定稿");
			historyList.add(jh);
		}
		if("正式通知发送".equals(jobType)){
			jh.setContent("发送了正式通知");
			//jh.setDetailURL(null);
			historyList.add(jh);
		}
	}

	private void setPreNoticeContent(JobHandleRecordsDTO jh, String jobType,
			ConferenceJobDataDTO conferenceJobDataDTO) {
		String underTaker = getUnderTaker(conferenceJobDataDTO);
		if("预通知起草".equals(jobType)){
			jh.setContent("起草了[预通知]");
			historyList.add(jh);
			setToapprovalContent(jh, underTaker,(long)(jh.getLevel()+1));
		}
		if("预通知修改".equals(jobType)){
			jh.setContent("修订了[预通知]提交给  "+ underTaker +" 进行审批");
			historyList.add(jh);
		}
		if("预通知定稿".equals(jobType)){
			jh.setContent("预通知定稿");
			historyList.add(jh);
		}
		if("预通知发送".equals(jobType)){
			jh.setContent("发送了预通知");
			//jh.setDetailURL(null);
			historyList.add(jh);
		}
	}

	private void setConferencePrepareContent(JobHandleRecordsDTO jh,
			String jobType) {
		if("会务准备".equals(jobType)){
			jh.setContent("会务准备情况");
			historyList.add(jh);
		}
	}

	private void setPlanContent(JobHandleRecordsDTO jh, String jobType,
			ConferenceJobDataDTO conferenceJobDataDTO) throws BusinessException {
		String underTaker = getUnderTaker(conferenceJobDataDTO);
		if("方案起草".equals(jobType)){
			jh.setContent("起草了[会议方案]");
			historyList.add(jh);
			setToapprovalContent(jh, underTaker);
		}
		if("方案修改".equals(jobType)){
			jh.setContent("修订了[会议方案]提交给  "+ underTaker +" 进行审批");
			historyList.add(jh);
		}
		if("方案定稿".equals(jobType)){
			jh.setContent("会议方案定稿");
			historyList.add(jh);
		}
	}

	/**
	 * @param jh
	 * @param underTaker
	 */
	private void setToapprovalContent(JobHandleRecordsDTO jh, String underTaker) {
		setToapprovalContent(jh,underTaker,null);
	}
	private void setToapprovalContent(JobHandleRecordsDTO jh, String underTaker,Long level) {
		JobHandleRecordsDTO missionCreateRecord1 = new JobHandleRecordsDTO();
		missionCreateRecord1.setActualEndTime(jh.getActualEndTime());
		missionCreateRecord1.setOperatorName(jh.getOperatorName());
//		if(jh.getJobType().contains("申请")){
//			missionCreateRecord1.setDetailURL(null);
		//}else{
			missionCreateRecord1.setDetailURL(jh.getDetailURL());
//		}
		missionCreateRecord1.setJobId(jh.getJobId());
		if(level == null){
			missionCreateRecord1.setLevel(jh.getLevel());
		}else{
			missionCreateRecord1.setLevel(level.intValue());
		}
		StringBuffer content1 = new StringBuffer("提交给");
		content1.append(" "+underTaker+" ");
		content1.append("进行审批");
		missionCreateRecord1.setContent(content1.toString());
		historyList.add(missionCreateRecord1);
	}

	private void setStartContent(JobHandleRecordsDTO jh, String jobType) {
		if("会议发起".equals(jobType)){
			jh.setContent("发起了["+missionDTO.getMissionName()+"]会议任务");
			jh.setDetailURL(null);
			historyList.add(jh);
		}
	}

	private void setOthersContent(JobHandleRecordsDTO jh, String jobType,
			ConferenceJobDataDTO conferenceJobDataDTO) throws BusinessException {
		String underTaker = getUnderTaker(conferenceJobDataDTO);
		if(jobType.contains("取回")){
            if(jobType.endsWith("取回")){
			   jh.setContent("取回了[ "+jobType.substring(0,jobType.length()-"取回".length())+"]");
            }
            if(jobType.startsWith("取回")){
               jh.setContent("取回了[ "+jobType.substring("取回".length())+"]");
            }
//			if(jobType.contains("申请")){
				//jh.setDetailURL(null);
//			}
			historyList.add(jh);
			 JobHandleRecordsDTO missionCreateRecord2 = new JobHandleRecordsDTO();
	            missionCreateRecord2.setActualEndTime(jh.getActualEndTime());
	            missionCreateRecord2.setOperatorName(jh.getOperatorName());
//				if(jobType.contains("申请")){
//		            missionCreateRecord2.setDetailURL(null);
//				}else{
		            missionCreateRecord2.setDetailURL(jh.getDetailURL());
//				}
	            missionCreateRecord2.setJobId(jh.getJobId());
	            missionCreateRecord2.setLevel(jh.getLevel());
	            StringBuffer content1 = new StringBuffer("发给");
	            content1.append(" "+underTaker+" ");
	            content1.append("进行审批");
	            missionCreateRecord2.setContent(content1.toString());
	            historyList.add(missionCreateRecord2);
		}
		if("退回".equals(conferenceJobDataDTO.getTurningAction())){
			JobDTO taskDTO = taskManagementWebService.findJobDetailByJobId(jh.getJobId());
			jh.setContent("退回给了" + taskDTO.getPreviousJobDTO().getUndertakerDTO().getUserName());
//			if(jobType.contains("申请")){
//				jh.setDetailURL(null);
//			}
			historyList.add(jh);
		}
		if("通过".equals(conferenceJobDataDTO.getTurningAction())){
			jh.setContent("同意定稿");
//			if(jobType.contains("申请")){
//				jh.setDetailURL(null);
//			}
			if(jobType.contains("申请")){
				jh.setContent("批准[会议申请]");
			}
			historyList.add(jh);
		}
		if("提交下一人审批".equals(conferenceJobDataDTO.getTurningAction())){
			StringBuffer content = new StringBuffer("同意并提交给");
	        content.append(" "+underTaker+" ");
	        content.append("进行审批");
	        jh.setContent(content.toString());
//	        if(jobType.contains("申请")){
//				jh.setDetailURL(null);
//			}
			historyList.add(jh);
		}
	}

	private void setApplyContent(JobHandleRecordsDTO jh, String jobType,
			ConferenceJobDataDTO conferenceJobDataDTO) {
		String underTaker = getUnderTaker(conferenceJobDataDTO);
		if("申请起草".equals(jobType)){
			jh.setContent("起草了[会议申请]");
			historyList.add(jh);
			setToapprovalContent(jh, underTaker);
		}
		if("修改会议申请".equals(jobType)){
			jh.setContent("修订了[会议申请]提交给  "+ underTaker +" 进行审批");
			//jh.setDetailURL(null);
			historyList.add(jh);
		}
		if("发起会议".equals(jobType)){
			jh.setContent("发起会议");
			//jh.setDetailURL(null);
			historyList.add(jh);
		}

		
	}

	public ConferenceStartingDTO getConferenceStartingDTO() {
		String conferenceType = conferenceStartingDTO.getConferenceType();
		if(conferenceStartingDTO !=null ){
			if("middleTypeConference".equalsIgnoreCase(conferenceType)){
				conferenceStartingDTO.setConferenceType("中型会议");
			}else if("smallTypeConference".equalsIgnoreCase(conferenceType)){
				conferenceStartingDTO.setConferenceType("小型会议");
			}else if("largeTypeConference".equalsIgnoreCase(conferenceType)){
				conferenceStartingDTO.setConferenceType("大型会议");
			}
		}
		return conferenceStartingDTO;
	}

	public void setConferenceStartingDTO(ConferenceStartingDTO conferenceStartingDTO) {
		this.conferenceStartingDTO = conferenceStartingDTO;
	}

	public String getImportanceLevel() {
		return importanceLevel;
	}

	public void setImportanceLevel(String importanceLevel) {
		this.importanceLevel = importanceLevel;
	}

	@Override
    public List<JobHandleRecordsDTO> getHistoryList() {
		return historyList;
	}

	@Override
    public void setHistoryList(List<JobHandleRecordsDTO> historyList) {
		this.historyList = historyList;
	}

    public String getConferenceApplicationModel() {
        return conferenceApplicationModel;
    }
	
	

}
